﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BMLib;

namespace BrainMatch
{
    public partial class NamesManagerDialog : Form
    {
        private WaveManager m_Manager = null;
        public NamesManagerDialog(WaveManager manager)
        {
            m_Manager = manager;

            InitializeComponent();
        }

        private void OnDeleteData(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.SelectedItems)
            {
                WaveDataSet.labelsRow row = item.Tag as WaveDataSet.labelsRow;
                if (row == null) continue;
                m_Manager.Dataset.labels.Rows.Remove(row);
                //m_Manager.DeleteLabel(row.id);
                listView1.Items.Remove(item);
            }
            //m_Manager.LoadLabels();
            m_Manager.SaveData();
            LoadData();
        }
        private void UpdateFromData(WaveDataSet.label_namesRow row)
        {
            textBox1.Tag = row;
            textBox1.Text = row.name;
            button1.Tag = row;
            button1.BackColor = BMUtility.StringToColor(row.color);
            LoadShapes(row.id);
        }

        private void OnSave(object sender, EventArgs e)
        {
            m_Manager.SaveData();
            button1.Enabled = false;
        }
        private void LoadShapes(int id)
        {
            listView1.Items.Clear();
            imageList1.Images.Clear();

            WaveItem wave = new WaveItem();
            DataRow[] rows=m_Manager.Dataset.labels.Select(string.Format("name_id={0}",id));
            Pen pen = new Pen(Color.FromArgb(100, 255, 0, 0),1);

            foreach (var item in rows)
            {
                ListViewItem li = new ListViewItem();
                li.ImageIndex = imageList1.Images.Count;
                li.Tag = item;

                double[] data = BMUtility.StringToDoubles(item["graphics_data"].ToString());
                Bitmap bitmap = new Bitmap(imageList1.ImageSize.Width, imageList1.ImageSize.Height, PixelFormat.Format24bppRgb);
                Graphics g = Graphics.FromImage(bitmap);
                g.Clear(Color.White);

                wave.SetData(data);
                wave.DrawToBitmap(bitmap, pen,true);
                imageList1.Images.Add(bitmap);

                listView1.Items.Add(li);
            }
        }
        private void LoadData() 
        {
            listView2.Items.Clear();
            foreach (var item in m_Manager.Dataset.label_names)
            {
                ListViewItem li = new ListViewItem();
                DataRow[] rows = m_Manager.Dataset.labels.Select(string.Format("name_id={0}", item.id));
                li.Text = string.Format("{0}({1})",item.name,rows.Length);
                li.Tag = item;
                listView2.Items.Add(li);
            }
 
        }
        private void NamesManagerDialog_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView2.SelectedItems.Count <= 0) return;
            WaveDataSet.label_namesRow row=listView2.SelectedItems[0].Tag as WaveDataSet.label_namesRow;
            if(row==null)return;
            UpdateFromData(row);
            button1.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                WaveDataSet.label_namesRow row = button1.Tag as WaveDataSet.label_namesRow;
                button1.BackColor = colorDialog1.Color;
                row.color = BMUtility.ColorToString(button1.BackColor);
                button1.Enabled = true;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            WaveDataSet.label_namesRow row = textBox1.Tag as WaveDataSet.label_namesRow;
            row.name = textBox1.Text;
            button1.Enabled = true;
        }
    }
}
